package com.example.backendhjh.controller;

import com.example.backendhjh.dto.CartItemDTO;
import com.example.backendhjh.dto.CartItemResponseDTO;
import com.example.backendhjh.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CartController {

    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    // 根据用户ID获取购物车
    @GetMapping("/cart")
    public List<CartItemResponseDTO> getCartByUser(@RequestParam Long userId) {
        return cartService.getCartByUser(userId);
    }

    // 添加商品到购物车
    @PostMapping("/cart")
    public CartItemResponseDTO addToCart(@RequestBody CartItemDTO cartItemDTO) {
        return cartService.addToCart(cartItemDTO);
    }

    // 更新购物车项数量
    @PutMapping("/cart/{id}/quantity")
    public CartItemResponseDTO updateQuantity(@PathVariable Long id, @RequestParam Integer quantity) {
        return cartService.updateQuantity(id, quantity);
    }

    // 从购物车中移除商品
    @DeleteMapping("/cart/{id}")
    public void removeFromCart(@PathVariable Long id) {
        cartService.removeFromCart(id);
    }

    // 清空用户购物车
    @DeleteMapping("/cart")
    public void clearCart(@RequestParam Long userId) {
        cartService.clearCart(userId);
    }
}
